using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SigmaStock_Components;
namespace project
{
    public partial class Pay_Publisher : Form
    {
        OleDbConnection conn;
        OleDbCommand command;
        string query;
        int amount_paid;
        int curr_total;
        
        public static int amt = 0; 

        public int curr_amt
        {
            set
            {
                amt = value;
            }
        }


        
         public Pay_Publisher()
        {
            query = "";
            InitializeComponent();
            dbconnection();
            int amount_due;            
            PNameText.Text = Form3.purchase_list[0].PNAME.ToString();
             query = "select AMOUNT_DUE from publisher_credit where PNAME='" + Form3.purchase_list[0].PNAME + "';";
            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                curr_total = int.Parse(reader[0].ToString());
            }
             int total=amt + curr_total;
             textBox2.Text = total.ToString();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        private void dbconnection()
        {
            try
            {
                conn = DBComponent.Mag_DealerDB.dbconnection();
                conn.Open();
                command = new OleDbCommand();
                command.Connection = conn;


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }

        private void PayButton_Click(object sender, EventArgs e)
        {
            if (!(String.IsNullOrEmpty(PayAmountText.Text)) && Validation.IsPositiveNumber(PayAmountText.Text))
            {
                int amount_due = int.Parse(textBox2.Text);
                amount_paid = int.Parse(PayAmountText.Text);
                if (amount_paid <= amount_due)
                {
                    SavePublisherReceipt();
                    Payment.UpdatePublisherDue();
                    Payment.UpdateMagazineBought();
                    UpdateTransactions();
                    UpdatePublisherCredit();
                    UpdateOrderMagazine();
                    Program.InvokeModal(this,"project.Publisher_Receipt");
                }
                else
                {
                    MessageBox.Show("Payment amount must be less than amount due !!! ");
                    PayAmountText.ResetText();
                }
                
            }
        }

        private void SavePublisherReceipt()
        {
            Publisher_Receipt_Object pub_rec = new Publisher_Receipt_Object();
            pub_rec.amount_paid = int.Parse(PayAmountText.Text);
            query = "select AMOUNT_DUE from publisher_credit where PNAME='" + PNameText.Text + "';";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            int amt_due = 0;
            while (reader.Read())
            {
                amt_due = int.Parse(reader[0].ToString());
            }
            pub_rec.current_due = amt_due+int.Parse(textBox2.Text)-pub_rec.amount_paid;
            pub_rec.pname = PNameText.Text;
            pub_rec.trans_date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            Form3.pub_receipt = pub_rec;
        
        }

        private void UpdateOrderMagazine()
        {
            command.Connection = conn;
            foreach (DBClass obj in Form3.purchase_list)
            {
                query = "insert into order_magazine(MNAME,MDATE,PNAME,QTY_ORDERED) VALUES('" + obj.MNAME + "','" + obj.MDATE + "','" + obj.PNAME + "','" + obj.qty + "');";
                command.CommandText = query;
                command.ExecuteNonQuery();
            }
        }

        private void UpdateTransactions()
        {
            //try
            //{
                string from = "KNA";
                string transaction_type = "Publisher_Payment";
                int amount = int.Parse(PayAmountText.Text);
                string to = PNameText.Text;
                query = "insert into transaction1(CURR_DATE,TRANSACTION_TYPE,TRANS_FROM,TRANS_TO,AMOUNT) VALUES('" + DateTime.Now + "','" + transaction_type + "','" + from + "','" + to + "','" + amount + "');";
                command = new OleDbCommand(query, conn);                
                command.ExecuteNonQuery();
            //}
            /*catch (Exception e)
            {
                Console.WriteLine(e);
            }*/
        }

        private void UpdatePublisherCredit()
        {
            int deposit = 0;
            int amount_due = 0;
            query = "select DEPOSIT,AMOUNT_DUE from publisher_credit where PNAME='" + Form3.purchase_list[0].PNAME + "';";
            command.CommandText = query;
            command.Connection = conn;
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                deposit = int.Parse(reader[0].ToString());
                amount_due = int.Parse(reader[1].ToString());
            }
            amount_due += amt;
            amount_due -= amount_paid;            
            reader.Close();
            query = "update publisher_credit set AMOUNT_DUE='" + amount_due + "' where PNAME='"+Form3.purchase_list[0].PNAME+"';";
            command.CommandText = query;
            command.Connection = conn;
            command.ExecuteNonQuery();            
        }

        private void Pay_Publisher_Load(object sender, EventArgs e)
        {

        }

        private void Pay_Publisher_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form3.DataReset();
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }
    }
}